<!--病例列表-表格-->
<template>
<div>
    <el-table
            :data="pageData"
            stripe
            style="width: 100%"
            @row-dblclick="handleView"
            v-loading="deleting"
    >
        <el-table-column
                label=""
                min-width="70">
            <template scope="scope">
                <el-button @click="handleView(scope.row,scope.$index)" size="small" >查看</el-button>
            </template>
        </el-table-column>
        <el-table-column
                prop="patientName"
                label="患者"
                min-width="60"
                v-if="!showName">
        </el-table-column>
         <el-table-column
                prop="caseName"
                label="诊断"
				min-width="170"
                :formatter="nameFormat">
        </el-table-column>
        <el-table-column
                prop="patientGender"
                :formatter="showsix"
                label="性别"
                min-width="50"
                v-if="!showName">
        </el-table-column>
        <el-table-column
                prop="patientAge"
                label="年龄"
                min-width="60">
        </el-table-column>
        <el-table-column
                prop="properties.hospitalize_time"
                :formatter="dateFormat"
                label="入院时间"
                min-width="90">
        </el-table-column>
        <el-table-column
                prop="properties.surgery_time"
                :formatter="dateSurgery"
                label="手术时间"
                min-width="90">
        </el-table-column>
        <el-table-column
                prop="properties.discharge_time"
                :formatter="dateFormat"
                label="出院时间"
                min-width="90">
        </el-table-column>
        <el-table-column
                prop="creatorDoctorName"
                label="创建医生"
                min-width="70">
        </el-table-column>
        <el-table-column
                prop="supervisorDoctorName"
                label="上级医生"
                min-width="70">
        </el-table-column>
    </el-table>
</div>
</template>
<script>

    import Vue from 'vue'
    import ElementUI from 'element-ui'
    import times from '../../../Resources/utils/wt-time-utils'
    import gender from '../../../Resources/utils/wt-form-utils'
	import Permission from 'winged/utils/wt-permission';
    Vue.use(ElementUI);
    export default {
        name: 'wt-case-table',
        props:{
            list: {
                type: Array,
                required: true
            },
            showName:{
                type: Boolean,
                default: true
            },
        },

        data() {
            return {
                // 当前页的列表数据
                pageData: this.list,
                deleting:false,
                allowEdit: Permission.checkPermission('patient-case-add'),
                allowRemove: Permission.checkPermission('patient-case-delete')
                // 当前的filter
            };
        },
        watch:{
            list: function () {
                this.pageData = this.list;
            }
        },
        methods: {
            dateFormat:function (row, column) {
                var date;
                if (column.property.indexOf('.')>0){
					if (column.label == '入院时间'&&row.properties.hospitalize_time!=undefined){
						date = row.properties.hospitalize_time;
                    }
                    else if(column.label == '出院时间'&&row.properties.discharge_time!=undefined){
						date = row.properties.discharge_time;
                    }
                }else {
					date = row[column.property];
                }
                if (date == undefined) {
                    return "";
                }
                return times.format(date, 'date')
            },
            dateSurgery:function (row, column) {
				var date;
				var surgery = [];
				if (row.properties.surgery_time !== undefined){
					var surgery_date = row.properties.surgery_time;
                    surgery_date.forEach(function(r){
					date = times.format(r, 'date');
					surgery.push(date);
				})
                return surgery.join("\n/\n")
                }

			},
            showsix:function(row, column){
                var six = row[column.property];
                return gender.formatGender(six);
            },
            handleView(row,index) {
                window.location.href = '/admin/patients/case/' + row.id;
            },
            nameFormat:function (row, column) {
                var arr = row[column.property].split(',') ;
                var str=arr.join("").replace(/\""/g, " / ");
                var str1=str.replace(/\"/g, "");
                return str1.replace('[','').replace(']','');
            },
            handleDelete:function (row,index) {
                var name=row.patientName;
                var arr=row.caseName.split(',');
                var str=arr.join("").replace(/\""/g, " / ");
                var str1=str.replace(/\"/g, "");
                var vm = this;
                this.$confirm('您将删除患者"' + name + '"的病例"'+str1.replace('[','').replace(']','')+'"，是否确定？', '操作提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.deleting = true;
                    vm.pageData.splice(index, 1);
                    require('winged/utils/axios');
                    let path = `patients/case/${row.id}`;
                    window.axios.delete(path).then(function (response) {
                        vm.deleting = false;
                        if(response.status==200){
                            vm.$message({
                                type: 'success',
                                message: '删除成功!'
                            });
                        }else {
                            vm.$message({
                                ype: 'error',
                                message: '删除失敗!'
                            });
                        }

                    });

                }).catch(() => {
                    vm.deleting = false;
                });
            },
            handedit:function (row) {
                window.location.href=`/admin/patients/case/${row.id}/edit`
            }
        },
        created(){
        	console.log(this.list)
        }
    }
</script>
